home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / FAST_LEX / SYM.C < prev    next >
Text File  |  1988-07-04  |  6KB  |  290 lines

  1. /* sym - symbol table routines */
  2.  
  3. /*
  4.  * Copyright (c) 1987, the University of California
  5.  * 
  6.  * The United States Government has rights in this work pursuant to
  7.  * contract no. DE-AC03-76SF00098 between the United States Department of
  8.  * Energy and the University of California.
  9.  * 
  10.  * This program may be redistributed.  Enhancements and derivative works
  11.  * may be created provided the new works, if made available to the general
  12.  * public, are made available for use by anyone.
  13.  */
  14.  
  15. #include "flexdef.h"
  16. /*
  17.  * MPW C 2.0.2 does not like variables named "entry".
  18.  */
  19. #ifdef MPW
  20. #define entry entry_by_another_name
  21. #endif
  22.  
  23. struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
  24. struct hash_entry *sctbl[START_COND_HASH_SIZE];
  25. struct hash_entry *ccltab[CCL_HASH_SIZE];
  26.  
  27. struct hash_entry *findsym();
  28.  
  29.  
  30. /* addsym - add symbol and definitions to symbol table
  31.  *
  32.  * synopsis
  33.  *    char sym[], *str_def;
  34.  *    int int_def;
  35.  *    hash_table table;
  36.  *    int table_size;
  37.  *    0 / -1 = addsym( sym, def, int_def, table, table_size );
  38.  *
  39.  * -1 is returned if the symbol already exists, and the change not made.
  40.  */
  41.  
  42. int addsym( sym, str_def, int_def, table, table_size )
  43. register char sym[];
  44. char *str_def;
  45. int int_def;
  46. hash_table table;
  47. int table_size;
  48.  
  49.     {
  50.     int hash_val = hashfunct( sym, table_size );
  51.     register struct hash_entry *entry = table[hash_val];
  52.     register struct hash_entry *new_entry;
  53.     register struct hash_entry *successor;
  54.     char *malloc();
  55.  
  56.     while ( entry )
  57.     {
  58.     if ( ! strcmp( sym, entry->name ) )
  59.         { /* entry already exists */
  60.         return ( -1 );
  61.         }
  62.     
  63.     entry = entry->next;
  64.     }
  65.  
  66.     /* create new entry */
  67.     new_entry = (struct hash_entry *) malloc( sizeof( struct hash_entry ) );
  68.  
  69.     if ( new_entry == NULL )
  70.     flexfatal( "symbol table memory allocation failed" );
  71.  
  72.     if ( (successor = table[hash_val]) )
  73.     {
  74.     new_entry->next = successor;
  75.     successor->prev = new_entry;
  76.     }
  77.     else
  78.     new_entry->next = NULL;
  79.  
  80.     new_entry->prev = NULL;
  81.     new_entry->name = sym;
  82.     new_entry->str_val = str_def;
  83.     new_entry->int_val = int_def;
  84.  
  85.     table[hash_val] = new_entry;
  86.  
  87.     return ( 0 );
  88.     }
  89.  
  90.  
  91. /* cclinstal - save the text of a character class
  92.  *
  93.  * synopsis
  94.  *    char ccltxt[];
  95.  *    int cclnum;
  96.  *    cclinstal( ccltxt, cclnum );
  97.  */
  98.  
  99. cclinstal( ccltxt, cclnum )
  100. char ccltxt[];
  101. int cclnum;
  102.  
  103.     {
  104.     /* we don't bother checking the return status because we are not called
  105.      * unless the symbol is new
  106.      */
  107.     char *copy_string();
  108.  
  109.     (void) addsym( copy_string( ccltxt ), (char *) 0, cclnum,
  110.            ccltab, CCL_HASH_SIZE );
  111.     }
  112.  
  113.  
  114. /* ccllookup - lookup the number associated with character class text
  115.  *
  116.  * synopsis
  117.  *    char ccltxt[];
  118.  *    int ccllookup, cclval;
  119.  *    cclval/0 = ccllookup( ccltxt );
  120.  */
  121.  
  122. int ccllookup( ccltxt )
  123. char ccltxt[];
  124.  
  125.     {
  126.     return ( findsym( ccltxt, ccltab, CCL_HASH_SIZE )->int_val );
  127.     }
  128.  
  129.  
  130. /* findsym - find symbol in symbol table
  131.  *
  132.  * synopsis
  133.  *    char sym[];
  134.  *    hash_table table;
  135.  *    int table_size;
  136.  *    struct hash_entry *entry, *findsym();
  137.  *    entry = findsym( sym, table, table_size );
  138.  */
  139.  
  140. struct hash_entry *findsym( sym, table, table_size )
  141. register char sym[];
  142. hash_table table;
  143. int table_size;
  144.  
  145.     {
  146.     register struct hash_entry *entry = table[hashfunct( sym, table_size )];
  147.     static struct hash_entry empty_entry =
  148.     {
  149.     (struct hash_entry *) 0, (struct hash_entry *) 0, NULL, NULL, 0,
  150.     } ;
  151.  
  152.     while ( entry )
  153.     {
  154.     if ( ! strcmp( sym, entry->name ) )
  155.         return ( entry );
  156.     entry = entry->next;
  157.     }
  158.  
  159.     return ( &empty_entry );
  160.     }
  161.  
  162.     
  163. /* hashfunct - compute the hash value for "str" and hash size "hash_size"
  164.  *
  165.  * synopsis
  166.  *    char str[];
  167.  *    int hash_size, hash_val;
  168.  *    hash_val = hashfunct( str, hash_size );
  169.  */
  170.  
  171. int hashfunct( str, hash_size )
  172. register char str[];
  173. int hash_size;
  174.  
  175.     {
  176.     register int hashval;
  177.     register int locstr;
  178.  
  179.     hashval = 0;
  180.     locstr = 0;
  181.  
  182.     while ( str[locstr] )
  183.     hashval = ((hashval << 1) + str[locstr++]) % hash_size;
  184.  
  185.     return ( hashval );
  186.     }
  187.  
  188.  
  189. /* ndinstal - install a name definition
  190.  *
  191.  * synopsis
  192.  *    char nd[], def[];
  193.  *    ndinstal( nd, def );
  194.  */
  195.  
  196. ndinstal( nd, def )
  197. char nd[], def[];
  198.  
  199.     {
  200.     char *copy_string();
  201.  
  202.     if ( addsym( copy_string( nd ), copy_string( def ), 0,
  203.          ndtbl, NAME_TABLE_HASH_SIZE ) )
  204.     synerr( "name defined twice" );
  205.     }
  206.  
  207.  
  208. /* ndlookup - lookup a name definition
  209.  *
  210.  * synopsis
  211.  *    char nd[], *def;
  212.  *    char *ndlookup();
  213.  *    def/NULL = ndlookup( nd );
  214.  */
  215.  
  216. char *ndlookup( nd )
  217. char nd[];
  218.  
  219.     {
  220.     return ( findsym( nd, ndtbl, NAME_TABLE_HASH_SIZE )->str_val );
  221.     }
  222.  
  223.  
  224. /* scinstal - make a start condition
  225.  *
  226.  * synopsis
  227.  *    char str[];
  228.  *    int xcluflg;
  229.  *    scinstal( str, xcluflg );
  230.  *
  231.  * NOTE
  232.  *    the start condition is Exclusive if xcluflg is true
  233.  */
  234.  
  235. scinstal( str, xcluflg )
  236. char str[];
  237. int xcluflg;
  238.  
  239.     {
  240.     char *copy_string();
  241.  
  242.     /* bit of a hack.  We know how the default start-condition is
  243.      * declared, and don't put out a define for it, because it
  244.      * would come out as "#define 0 1"
  245.      */
  246.     /* actually, this is no longer the case.  The default start-condition
  247.      * is now called "INITIAL".  But we keep the following for the sake
  248.      * of future robustness.
  249.      */
  250.  
  251.     if ( strcmp( str, "0" ) )
  252.     printf( "#define %s %d\n", str, lastsc * 2 );
  253.  
  254.     if ( ++lastsc >= current_max_scs )
  255.     {
  256.     current_max_scs += MAX_SCS_INCREMENT;
  257.  
  258.     ++num_reallocs;
  259.  
  260.     scset = reallocate_integer_array( scset, current_max_scs );
  261.     scbol = reallocate_integer_array( scbol, current_max_scs );
  262.     scxclu = reallocate_integer_array( scxclu, current_max_scs );
  263.     actvsc = reallocate_integer_array( actvsc, current_max_scs );
  264.     }
  265.  
  266.     if ( addsym( copy_string( str ), (char *) 0, lastsc,
  267.      sctbl, START_COND_HASH_SIZE ) )
  268.     lerrsf( "start condition %s declared twice", str );
  269.  
  270.     scset[lastsc] = mkstate( SYM_EPSILON );
  271.     scbol[lastsc] = mkstate( SYM_EPSILON );
  272.     scxclu[lastsc] = xcluflg;
  273.     }
  274.  
  275.  
  276. /* sclookup - lookup the number associated with a start condition
  277.  *
  278.  * synopsis
  279.  *    char str[], scnum;
  280.  *    int sclookup;
  281.  *    scnum/0 = sclookup( str );
  282.  */
  283.  
  284. int sclookup( str )
  285. char str[];
  286.  
  287.     {
  288.     return ( findsym( str, sctbl, START_COND_HASH_SIZE )->int_val );
  289.     }
  290.